package com.canbot.u05.activity.admin

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.view.View
import android.widget.Toast
import com.alibaba.fastjson.JSON
import com.canbot.u05.MainApplication
import com.canbot.u05.R
import com.canbot.u05.activity.BaseActivityNoTitle
import com.canbot.u05.activity.customvoice.tool.RecyclerItemDecoration
import com.canbot.u05.activity.loge
import com.canbot.u05.adapter.AdminFaceGroupAdapter
import com.canbot.u05.bean.JsonResponse
import com.canbot.u05.bean.PersonGroupData
import com.canbot.u05.socket.IpMsgConst
import com.canbot.u05.utils.OkHttpUtils
import kotlinx.android.synthetic.main.activity_admin_face_group.*
import java.util.*

/**
 * desc: 管理员人脸分组选择界面
 * author: xxl
 * date: 2019/6/19
 */
class AdminFaceGroupActivity : BaseActivityNoTitle(), AdminFaceGroupAdapter.OnItemClickListener {


    private lateinit var adminFaceGroupAdapter: AdminFaceGroupAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_admin_face_group)
        val groupList = ArrayList<PersonGroupData>()
        val layoutManager = GridLayoutManager(this, 5)
        recycle_face_group.layoutManager = layoutManager
        recycle_face_group.addItemDecoration(RecyclerItemDecoration(5, 0, resources.getDimension(R.dimen.px80).toInt(), false))
        adminFaceGroupAdapter = AdminFaceGroupAdapter(this, groupList)
        adminFaceGroupAdapter.setItemClickListener(this)
        recycle_face_group.adapter = adminFaceGroupAdapter
        getData()
    }

    /**
     * 获取人脸分组信息
     */
    private fun getData() {

        val hashMap = HashMap<String, Any>()
        hashMap["face"] = ""
        OkHttpUtils.upLoadFile("http://" + IpMsgConst.CHEST_SOCKET_HOST + ":7755/" + "faceGroupList", hashMap, object : OkHttpUtils.OkHttpCallBack {
            override fun onSuccess(result: String) {
                loge("onSuccess")

                val strMsgBean = JSON.parseObject(result, JsonResponse::class.java)
                if (strMsgBean.rc == 0) {
                    val jsonStr = strMsgBean.result.toString()
                    val personGroupData = JSON.parseArray(jsonStr, PersonGroupData::class.java)
                    runOnUiThread { adminFaceGroupAdapter.setGroupList(personGroupData) }
                }
            }

            override fun onFailure() {
                loge("获取人脸数据失败")
                Toast.makeText(this@AdminFaceGroupActivity, "网络错误！", Toast.LENGTH_SHORT).show()
            }
        })
    }

    fun back(view: View) = finish()


    override fun onItemClick(groupId: Int) {
        loge("groupId:$groupId")
        val intent = Intent(this, AdminFaceChooseActivity::class.java)
        intent.putExtra("groupId", groupId)
        startActivityForResult(intent, AdminFaceModifyActivity.REQUEST_CODE)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == AdminFaceModifyActivity.RESULT_OK) {
            setResult(resultCode, data) //返回选中的人脸数据
            finish()
        }
    }
}
